﻿namespace SpImageCasheSDK
{
    public class SpImageCacheData
    {
        public static string SpImagecacheLibraryName = "imagecache";

        public string OriginalLibraryName;

        public string PresetName;

        public string RelativeFilePath;

        public string RelativeFoldersPath;

        public string ImageCacheFilePath;

        public string OriginalFilePath;

        public SpImageCacheData(string requestPath)
        {
            if (requestPath.Contains(SpImagecacheLibraryName))
            {
                string relativeRequestPath = requestPath.Substring(requestPath.IndexOf(SpImagecacheLibraryName));

                string[] tmp = relativeRequestPath.Split('/');

                if (tmp.Length > 3)
                {
                    ImageCacheFilePath = requestPath;

                    PresetName = tmp[1];

                    OriginalLibraryName = tmp[2];

                    RelativeFilePath = relativeRequestPath.Substring(relativeRequestPath.IndexOf(OriginalLibraryName) + OriginalLibraryName.Length + 1);

                    string[] folders = RelativeFilePath.Trim('/').Split('/');

                    RelativeFoldersPath = folders.Length > 1 ? string.Join("/", folders, 0, folders.Length - 1) : string.Empty;

                    OriginalFilePath = requestPath.Replace(string.Format("/{0}/{1}", SpImagecacheLibraryName, PresetName), string.Empty);
                }
            }
        }
    }
}
